/*
 * @Author: Starfish_DL
 * @Date: 2020-11-06 23:47:15
 * @LastEditors: Starfish_DL
 * @LastEditTime: 2020-11-10 15:41:23
 * @CurrentVersion: 1.0
 * @Description: 
 */

public class RunnableDemo implements Runnable {
    private Thread t;
    private String threadName;

    public RunnableDemo(String threadName) {
        this.threadName = threadName;
        System.out.println("Creating " + this.threadName);
    }

    @Override
    public synchronized void run() {
        System.out.println("Running " + this.threadName);
        try {
            for (int i = 4; i > 0; i--) {
                System.out.println("Thread: " + this.threadName + "," + i);
                Thread.sleep(50);
            }
        } catch (InterruptedException e) {
            System.out.println("Thread: " + this.threadName + " interrupted.");
            // done: handle exception
        } finally {
            System.out.println("Thread: " + this.threadName + " finished.");
        }
    }
    
    public void start() {
        System.out.println("Starting " + this.threadName);
        if (t == null) {
            t = new Thread(this, this.threadName);
            t.start();
        }
    }
}
